<script type="text/javascript" src="${baseURL }/component/select2/js/select2.js"></script>
<script type="text/javascript" src="${baseURL }/component/select2/js/sd.select2.js"></script>
<link href="${baseURL }/component/select2/css/select2.css" rel="stylesheet" />
<style>
.select2-container{
	z-index: 999999;
}
.select2-container--default .select2-selection--multiple {
    background-color: white;
    border:  1px solid #aaa;
    cursor: text;
    height: 200px;
}
.select2-container--default.select2-container--focus .select2-selection--multiple{
	border: 0px;
}
.all-height{
	height: 100%;
}
</style>
<%@ page language="java" pageEncoding="UTF-8"%>
<!-- 明细与列表在同一个页面 -->
<div id="detail" style="display: none;">
	<form id="formdetail" name="formdetail" method="post">
	</form>
</div>

<script type="text/javascript">
	//编辑，新增弹出框
	var detailWin = null, curentData = null, currentIsAddNew;
	var formdetail = $("#formdetail");
	var form = null;
	var $multiSelect;
	function FF_ShowDialog(data, isAddNew) {
		curentData = data;
		currentIsAddNew = isAddNew;
		if (detailWin) {
			detailWin.show();
		} else {
			//创建表单结构, 这里的字段命名为了与同其它地方区分，加了SDdetail_前缀，后台获取数据时也得去掉前缀(若明细与列表不在同一页面时，可不用)
			form = formdetail.ligerForm({
				fields : [ {
					name : "SDdetail_id",
					type : "hidden"
				},
				{
					display : "编码",
					name : "SDdetail_code",
					newline : true,
					labelWidth : 100,
					width : 420,
					space : 30,
					type : "text",
					labelAlign : "right",
					validate : {
						required : true,
						maxlength : 128
					}
				},
				{
					display : "名称",
					name : "SDdetail_name",
					newline : true,
					labelWidth : 100,
					width : 420,
					space : 30,
					type : "text",
					labelAlign : "right",
					validate : {
						required : true,
						maxlength : 128
					}
				},{
					display : "模板页面",
					name : "viewsIdDesc",
					newline : true,
					labelWidth : 100,
					width : 420,
					space : 30,
					type : "text",
					labelAlign : "right"}],
				toJSON : JSON2.stringify
			});

			detailWin = $.ligerDialog.open({
				title : '模板编辑',
				target : $("#detail"),
				width : 650,
				height : 400,
				isShade : true,
				onClose : function() {
					detailWin = null;
				},
				buttons : [ {
					text : '取消',
					className: 'fa fa-close',
					onclick : function() {
						detailWin.hide();
					}
				},  {
					text : '保存',
					className: 'fa fa-check',
					onclick : function() {
						FF_Save();
					}
				} ]
			});
			$("input[name='viewsIdDesc']").hide();
			sd.select2.ajaxSelect({
				 container : $("input[name='viewsIdDesc']").parent(),	//select2容器
				 dataUrl : baseURL + "/report/commsearch/commsearchlist/listJSONDataAll.do",	//服务器数据源请求url
				 dynamic : false, 	//是否可动态添加选项item
				 ajaxCallback : function($multiSelect2){
					 $multiSelect = $multiSelect2;
					 initTag($("input[name='viewsIdDesc']").val(),$multiSelect);
					 $("input[name='viewsIdDesc']").parents("li:eq(0)").height("200px");
					 $("input[name='viewsIdDesc']").parent().addClass("all-height");
				 }, 	//回调函数
				 placeholder : "请选择报表"
			 });
		}

		if (curentData) {
			form._setData({'SDdetail_id':curentData.id,'SDdetail_name':curentData.name,'SDdetail_code':curentData.code,"viewsIdDesc":curentData.viewsIdDesc});
		} else {
			form._setData({'SDdetail_id':"",'SDdetail_name':"",'SDdetail_code':"","viewsIdDesc":""});
		}
		if($multiSelect){
			initTag($("input[name='viewsIdDesc']").val(),$multiSelect);
			$("input[name='viewsIdDesc']").parents("li:eq(0)").height("200px");
			$("input[name='viewsIdDesc']").parent().addClass("all-height");
		}
	}
	
	jQuery.metadata.setType("attr", "validate");
	LG.validate(formdetail);
	
	 function initTag(report,$multiSelect2) {
		 if (report && report != null && report != "") {
			 var select2Val = new Array();
			 var tags = report.split(";");
			 for (var i = 0; i < tags.length; i++) {
				 select2Val.push(tags[i]);
			 }
			 $multiSelect2.val(select2Val).trigger("change");
		 }else{
			 $multiSelect2.val("").trigger("change");
		 }
	 }
	 
	 jQuery.validator.addMethod("unique", function(value, element) {
			var flag = validUnique(value);
			return this.optional(element) || !flag;
		}, "已存在，不能重复添加");
	 
	 function validUnique(value) {
			var flag = false;
			var url = "${baseURL }/report/template/report/reporttemplateedit/validUnique.do";
			$.ajax({
				async : false,
				url : url,
				dataType : "json",
				data : {
					code : value,
					id : $("input[name='SDdetail_id']").val()
				},
				contentType : "application/x-www-form-urlencoded;charset=UTF-8",
				success : function(data, status) {
					if (data) {
						flag = true;
					} else {
						flag = false;
					}
				}
			});
			return flag;
		}
	
	function FF_Save() {
		formdetail.attr("action",
				"${baseURL}/report/template/report/reporttemplatesave/save.do");
		LG.submitForm(formdetail,
			function(data) {
				if (data && data.hasOk) {
					grid.loadData();
					detailWin.hide();
					win.$.ligerDialog.success('操作成功!');
				} else {
					win.$.ligerDialog.error("操作失败!失败信息如下:\n" + data.message
							+ "\n如您对以上信息有疑问，请联系管理人员!");
				}
			});
	}
</script>
